package com.longjianghuasheng.logistics.business.register

import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.longjianghuasheng.logistics.entity.LoginResponse
import com.longjianghuasheng.logistics.ext.*
import com.longjianghuasheng.logistics.ext.commit
import com.longjianghuasheng.logistics.network.apiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class RegisterViewModel : BaseViewModel() {

    val result by lazy {
        MutableLiveData<LoginResponse>()
    }

    val mobileObservableField   by lazy {
        ObservableField("")
    }

    val codeObservable by lazy {
        ObservableField("")
    }

    val startCount by lazy {
        ObservableInt()
    }

    val passwordObservable by lazy {
        ObservableField("")
    }

    fun onGetCode() {
        val mobile = mobileObservableField.get()
        if (!checkPhone(mobile)) return
        commit({ apiService.get_code(mobile?:"",1)},{
            startCount.set(60)
        })
    }

    fun register() {
        val mobile = mobileObservableField.get()
        val password = passwordObservable.get()
        val code = codeObservable.get()
        if (!checkPhone(mobile)) return
        if (!checkPwd(password))return
        if (code.isNullOrBlank()) {
            toastNormal("请输入正确的验证码")
            return
        }
        commit2({ apiService.register(mobile?:"",password?:"",
            password?:"",code,1)},result)
    }
}